package com.core.sgip.common.factory;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import com.core.sgip.common.SGIPConstant;
import com.core.sgip.common.SGIPMsg;
import com.core.sgip.common.SGIPUtils;
import com.core.sgip.common.body.SGIPCommand;
import com.core.sgip.common.body.command.Bind;
import com.core.sgip.common.body.command.BindResp;
import com.core.sgip.common.body.command.Deliver;
import com.core.sgip.common.body.command.DeliverResp;
import com.core.sgip.common.body.command.Report;
import com.core.sgip.common.body.command.ReportResp;
import com.core.sgip.common.body.command.Submit;
import com.core.sgip.common.body.command.SubmitResp;
import com.core.sgip.common.body.command.UnBind;
import com.core.sgip.common.body.command.UnBindResp;
import com.core.sgip.common.head.SGIPMsgHead;

public class SGIP2DownFactory {

	public static SGIPMsg constructSGIPMsg(byte[] source) throws IOException {
		SGIPMsg sgipMsg = new SGIPMsg();
		if (source != null && source.length >= 20) {
			sgipMsg.initPropertiesByBytes(source);
		}
		return sgipMsg;
	}

	public static SGIPMsg getSGIPMsg(long commandId) throws Exception {
		SGIPMsg sgipMsg = new SGIPMsg();
		SGIPMsgHead head = new SGIPMsgHead(commandId);
		sgipMsg.setHead(head);
		SGIPCommand command = getCommandByCommandId(commandId);
		sgipMsg.setCommand(command);
		return sgipMsg;
	}
	public static SGIPMsg getSGIPMsg(long commandId,List<String> listUserNumber,String content) throws Exception {
		SGIPMsg sgipMsg = new SGIPMsg();
		SGIPMsgHead head = new SGIPMsgHead(commandId);
		sgipMsg.setHead(head);
		SGIPCommand command = null;
		Deliver deliver  =  new Deliver();
		deliver.setUserNumber(listUserNumber.get(0));
		deliver.setMessageContent(content);
		command  = deliver;
		sgipMsg.setCommand(command);
		return sgipMsg;
	}
	public static SGIPMsg getSGIPMsg(long commandId, List<String> listUserNumber,String customerNo, String lastState, String lastDesc,
			Date lastSendDate) throws Exception {
		SGIPMsg sgipMsg = new SGIPMsg();
		SGIPMsgHead head = new SGIPMsgHead(commandId);
		sgipMsg.setHead(head);
		SGIPCommand command = null;
		Report report = new Report();
		

		report.setSubmitSequenceNumber(Long.valueOf(customerNo.substring(0, 10)),
				Long.valueOf(customerNo.substring(10, (customerNo.length() - 1))),
				Long.valueOf(String.valueOf(customerNo.charAt(customerNo.length() - 1))));
		report.setState("lastState".equals("Y") ? 0 : 2);
		report.setUserNumber(listUserNumber.get(0));
		command = report;	
		sgipMsg.setCommand(command);
		return sgipMsg;
	}
	public static SGIPMsg getSGIPMsg(long commandId,String result,String msg) throws Exception {
		SGIPMsg sgipMsg = new SGIPMsg();
		SGIPMsgHead head = new SGIPMsgHead(commandId);
		sgipMsg.setHead(head);
		SGIPCommand command = getCommandByCommandId(commandId,result,msg);
		sgipMsg.setCommand(command);
		return sgipMsg;
	}
	public static SGIPMsg getSGIPMsg(String appkey,String password,long commandId) throws Exception {
		SGIPMsg sgipMsg = new SGIPMsg();
		SGIPMsgHead head = new SGIPMsgHead(commandId);
		sgipMsg.setHead(head);
		SGIPCommand command = getCommandByCommandId(appkey,password,commandId);
		sgipMsg.setCommand(command);
		return sgipMsg;
	}

	public static SGIPCommand getCommandByCommandId(long commandId) throws Exception {
		SGIPCommand command = null;
		if (SGIPConstant.SGIP_BIND == commandId) {
			
			Bind bind = new Bind();
			command = bind;
			
		} else if (SGIPConstant.SGIP_BIND_RESP == commandId) {
			command = new BindResp();
		} else if (SGIPConstant.SGIP_SUBMIT == commandId) {
			command = new Submit();
		} else if (SGIPConstant.SGIP_SUBMIT_RESP == commandId) {
			command = new SubmitResp();
		} else if (SGIPConstant.SGIP_DELIVER == commandId) {
			command = new Deliver();
		} else if (SGIPConstant.SGIP_DELIVER_RESP == commandId) {
			command = new DeliverResp();
		} else if (SGIPConstant.SGIP_REPORT == commandId) {
			command = new Report();
		} else if (SGIPConstant.SGIP_REPORT_RESP == commandId) {
			command = new ReportResp();
		} else if (SGIPConstant.SGIP_UNBIND == commandId) {
			command = new UnBind();
		} else if (SGIPConstant.SGIP_UNBIND_RESP == commandId) {
			command = new UnBindResp();
		} else {
			throw new Exception("command is not exist ; commandId:" + commandId);
		}
		return command;
	}

	public static SGIPCommand getCommandByCommandId(long commandId, String result, String msg) throws Exception {
		SGIPCommand command = null;

		if (SGIPConstant.SGIP_BIND == commandId) {
			command = new Bind();
		} else if (SGIPConstant.SGIP_BIND_RESP == commandId) {
			BindResp bindResp = new BindResp();
			if ("1".equals(result)) {
				bindResp.setResult(1);
				bindResp.setReservestr(msg);
			}
			command = bindResp;
		} else if (SGIPConstant.SGIP_SUBMIT == commandId) {
			command = new Submit();
		} else if (SGIPConstant.SGIP_SUBMIT_RESP == commandId) {
			SubmitResp sbumitResp = new SubmitResp();
			if ("1".equals(result)) {
				sbumitResp.setResult(1);
			}
			command = sbumitResp;
		} else if (SGIPConstant.SGIP_DELIVER == commandId) {
			command = new Deliver();
		} else if (SGIPConstant.SGIP_DELIVER_RESP == commandId) {
			DeliverResp deliverResp = new DeliverResp();
			if ("1".equals(result)) {
				deliverResp.setResult(1);
			}
			command = deliverResp;
		} else if (SGIPConstant.SGIP_REPORT == commandId) {
			command = new Report();
		} else if (SGIPConstant.SGIP_REPORT_RESP == commandId) {
			command = new ReportResp();
		} else if (SGIPConstant.SGIP_UNBIND == commandId) {
			command = new UnBind();
		} else if (SGIPConstant.SGIP_UNBIND_RESP == commandId) {
			command = new UnBindResp();
		} else {
			throw new Exception("command is not exist ; commandId:" + commandId);
		}
		return command;
	}
	public static SGIPCommand getCommandByCommandId(String appkey, String password,long commandId) throws Exception {
		SGIPCommand command = null;
		
		if (SGIPConstant.SGIP_BIND == commandId) {
			Bind bind = new Bind();
			bind.setLoginName(appkey);
			bind.setLoginPassWord(password);
			command = bind;
		}  else {
			throw new Exception("command is not exist ; commandId:" + commandId);
		}
		return command;
	}

}
